const db = require('../db/index')
const bcrypt = require('bcryptjs')

// 获取用户基本信息
exports.getUserInfo = (req, res) => {
    console.log(req.user.id.length, 'id');
    
    // 获取学生信息
    if (req.user.id.length >= 11) {
        const sql = 'select id,username,nickname,user_pic,email from student where id=?'
        getInfoHandler(req, res, sql)
    // 获取老师信息
    } else if (11 > req.user.id.length && req.user.id.length > 3) {
        const sql2 = 'select id,username,nickname,user_pic,email,job from teacher where id=?'
        getInfoHandler(req, res, sql2)
        // 获取管理员信息
    } else {
        const sql3 = 'select id,username from manager where id=?'
        getInfoHandler(req, res, sql3)
    }
}
// 获取用户信息函数
const getInfoHandler = (req, res, sql) => {
    db.query(sql, req.user.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取用户信息失败')
        res.send({
        status: 0,
        message: '获取用户信息成功！',
        data: results[0]
        })
    })
}


// 修改用户基本信息
exports.updateUserInfo = (req, res) => {
    // 修改学生信息
    if (req.user.id.length >= 11) {
        const sql = 'update student set ? where id=?'
        updataInfoHandler(req, res, sql)
    // 修改老师信息
    } else {
        const sql2 = 'update teacher set ? where id=?'
        updataInfoHandler(req, res, sql2)
    }
}
// 修改用户信息函数
const updataInfoHandler = (req, res, sql) => {
    db.query(sql, [req.body, req.user.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('修改用户信息失败')
        return res.cc('修改用户信息成功！', 0)
    })
}

// 修改密码
exports.updatePsd = (req, res) => {
    // 修改学生密码
    if (req.user.id.length >= 11) {
        const sql = 'select * from student where id=?'
        updatePsdHandler(req, res, sql, 'student')
    // 修改老师密码
    } else {
        const sql2 = 'select * from teacher where id=?'
        updatePsdHandler(req, res, sql2, 'teacher')
    }
    
}
// 修改密码的函数
const updatePsdHandler = (req, res, sql, user) => {
    db.query(sql, req.user.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('用户不存在！')
        // TODO:判断提交的旧密码是否正确
        const comparRes = bcrypt.compareSync(req.body.oldPassword, results[0].password)
        if (!comparRes) return res.cc('原密码错误！')
        // 原密码正确，对密码进行加密后存入数据库
        const newPassword = bcrypt.hashSync(req.body.newPassword, 10)
        const sql = `update ${user} set password=? where id=?`
        db.query(sql, [newPassword, req.user.id], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('更新密码失败！')
            res.cc('更新密码成功！', 0)
        })
    })
}


// 更换头像
exports.updateAvatar = (req, res) => {
    // 更换学生头像
    if (req.user.id.length >= 11) {
        const sql = 'update student set user_pic=? where id=?'
        updateAvaHandler(req, res, sql)
    } else {
        const sql = 'update teacher set user_pic=? where id=?'
        updateAvaHandler(req, res, sql)
    }
    
}
// 更换头像函数
const updateAvaHandler = (req, res, sql) => {
    db.query(sql, [req.body.avatar, req.user.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('更新用户头像失败！')
        res.cc('更新用户头像成功！', 0)
    })
}